
//
//  BFPViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/6/17.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BFPViewController.h"
#import "BFPTableViewCell.h"
#import "DianZZiViewController.h"

@interface BFPViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *leftArr;
@property (nonatomic, strong) UIView *tabfootveiw;
@property (nonatomic, strong) UIView *butView;
@property (nonatomic, strong) NSString *urlstr;

@end

@implementation BFPViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发票详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];

    self.leftArr = [NSMutableArray arrayWithCapacity:1];
    self.dataSource = [NSMutableArray arrayWithCapacity:1];
    
    NSArray *arr = @[@"发票抬头",@"发票类型",@"发票内容",@"发票金额",@"申请时间",@"电子邮箱"];
    [self.leftArr addObjectsFromArray:arr];
    
    [self requestData];
}

- (void)requestData {
    [self.dataSource removeAllObjects];
    kWeakSelf;
    [NetWorkTool POST:getinvoiceInfo param:nil success:^(id dic) {
        KMyLog(@"sfadas%@",dic);
        NSDictionary *muydic = [dic objectForKey:@"data"];
        KMyLog(@"ruehuwfh%@",muydic);
        [self.dataSource addObject:[muydic objectForKey:@"title"]];
        [self.dataSource addObject:@"电子发票"];
        [self.dataSource addObject:@"会费"];
        [self.dataSource addObject:weakSelf.muarr[0]];
        [self.dataSource addObject:weakSelf.muarr[2]];
        [self.dataSource addObject:[muydic objectForKey:@"email"]];
        weakSelf.urlstr = [NSString stringWithFormat:@"%@",[muydic objectForKey:@"einvoice_img_url"]];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark -- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BFPTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BFPTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.leftLB.text = [_leftArr safeObjectAtIndex:indexPath.row];
    cell.rightLB.text = [_dataSource safeObjectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark -- 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, ScreenW, ScreenH-kNaviHeight) style:(UITableViewStylePlain)];
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
        if (iOS11) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.tableFooterView = self.butView;
        _tableView.tableHeaderView = self.tabfootveiw;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 120;
        [_tableView registerNib:[UINib nibWithNibName:@"BFPTableViewCell" bundle:nil] forCellReuseIdentifier:@"BFPTableViewCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataSource;
}

-(UIView *)tabfootveiw{
    if (!_tabfootveiw) {
        _tabfootveiw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 57)];
        UIView *he = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 5)];
        he.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [_tabfootveiw addSubview:he];
        
        UILabel *titiLB = [[UILabel alloc]initWithFrame:CGRectMake(16, 27, 100, 16)];
        titiLB.font = FontSize(16);
        titiLB.textColor = [UIColor colorWithHexString:@"#232620"];
        titiLB.text = @"发票信息";
        [_tabfootveiw addSubview:titiLB];
        UILabel *titiLBline = [[UILabel alloc]initWithFrame:CGRectMake(16, 56, KWIDTH-16, 1)];
        titiLBline.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];        [_tabfootveiw addSubview:titiLBline];
    }
    return _tabfootveiw;
}

-(UIView *)butView{
    if (!_butView) {
        _butView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 64)];
        _butView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        UIButton *lookBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        lookBut.frame =CGRectMake(0, 20, KWIDTH, 44);
        [lookBut setTitle:@"查看电子发票 " forState:(UIControlStateNormal)];
        lookBut.titleLabel.font = FontSize(12);
        [lookBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [lookBut setBackgroundColor:zhutiColor];
        [_butView addSubview:lookBut];
        lookBut.userInteractionEnabled = YES;
        [lookBut addTarget:self action:@selector(lookAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _butView;
}

-(void)lookAction:(UIButton *)but{
    KMyLog(@"查看电子发票");
    DianZZiViewController *look  = [[DianZZiViewController alloc]init];
    look.urlstr = _urlstr;
    
    if ([self.urlstr isEqualToString:@""]) {
        ShowToastWithText(@"暂未上传电子发票");
    } else {
        [self.navigationController pushViewController:look animated:YES];
    }    
}

@end
